﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMSConsulting.SMSLIB.Utilities;

namespace Access.PowerTools.Utilities
{
    public class FilesAndFolders
    {
        public static void ClearFolder(string backUpFolderPath)
        {
            RunTimeAssert.FolderExists(backUpFolderPath);
            string[] files = System.IO.Directory.GetFiles(backUpFolderPath);

            foreach (string file in files)
            {
                System.IO.File.Delete(file);
            }
        }

        public static void BackUpFile(string sourceFileFullPath)
        {
            string fileNameNoExt = System.IO.Path.GetFileNameWithoutExtension(sourceFileFullPath);
            string folder = System.IO.Path.GetDirectoryName(sourceFileFullPath);
            string fileExt = System.IO.Path.GetExtension(sourceFileFullPath);
            string backUpFileFullPath = "";

            int version = 0;
            bool exists = false;
            do
            {
                version++;
                backUpFileFullPath =
                    System.IO.Path.Combine(folder, 
                       fileNameNoExt + ";" + version.ToString() + fileExt);

                exists = System.IO.File.Exists(backUpFileFullPath); ;
                
            } while (exists);

            System.IO.File.Copy(sourceFileFullPath, backUpFileFullPath);    
        }
    }
}
